<<template>
	<el-dialog :title="title" :visible.sync="show" width="85%" :close-on-click-modal='false'>
    <div style="position: relative"> 
        <div style="margin-bottom: 20px;">
          <label>文章名称：</label>
            <el-input
              placeholder="请输入文章名称"
              v-model="form.name"
              style="width: 400px; margin-left: 15px;">
            </el-input>
        </div>
        <div style="margin-bottom: 20px;">
          <label>文章标题：</label>
            <el-input
              placeholder="请输入文章标题"
              v-model="form.title"
              style="width: 400px; margin-left: 15px;">
            </el-input>
        </div>
        <div v-html="form.content" class="h-content"></div>
        <!-- <quill-editor ref="editor" v-model="form.content"
          :options="editorOption"
          @change="onEditorChange($event)">
        </quill-editor> -->
    </div>
	  <div slot="footer" class="dialog-footer">
	    <el-button @click="show = false">取 消</el-button>
      <el-button @click="save">确定</el-button>
      <!-- <el-button @click="publish">发布</el-button> -->
	  </div>
	</el-dialog>
</template>
<script>
import api from "../../../api/api";
// import { quillEditor } from "vue-quill-editor";
export default {
  props: {
    id: null
  },
  created() {
    this.getDetail();
  },
  components: {
    // quillEditor
  },
  computed: {
    title() {
      return this.id ? "编辑文章" : "新建文章";
    }
  },
  data() {
    return {
      show: true,
      form: {
        name: "",
        ttile: "",
        content: ""
      },
      editorOption: {
        // some quill options
      }
    };
  },
  methods: {
    getDetail() {
      if (this.id) {
        api
          .getDataByID({
            tableName: "news",
            id: this.id
          })
          .then(res => {
            if (res.ret == 0) {
              //debugger;
              this.form = res.data;
            } else {
              this.$message({
                type: "error",
                message: "查询文章信息失败！"
              });
            }
          })
          .catch(e => {
            this.$message({
              type: "error",
              message: "查询文章信息失败！"
            });
          });
      }
    },
    onEditorChange({ editor, html, text }) {},
    save() {
      if (
        this.$refs.editor.quillEditor.getText().length > 0 &&
        this.form.name.length > 0 &&
        this.form.title.length > 0
      ) {
        if (this.id) {
          form.id = this.id;
        }
        api
          .addOrEditSubmit({
            tableName: "news",
            form: form
          })
          .then(res => {
            if (res.ret == 0) {
              this.$message({
                type: "success",
                message: "添加或者更新文章成功！"
              });
              this.show = false;
              this.$emit("refresh");
            } else {
              this.$message({
                type: "error",
                message: "添加或者更新文章失败！"
              });
            }
          })
          .catch(e => {
            this.$message({
              type: "error",
              message: "添加或者更新文章失败！"
            });
          });
      }
      //console.log(this.content);
    },
    publish() {
      if (this.id) {
        api
          .publish({
            ids: [this.id]
          })
          .then(res => {
            if (res.ret == 0) {
              this.$message({
                type: "success",
                message: "文章推送成功！"
              });
            } else {
              this.$message({
                type: "error",
                message: res.msg
              });
            }
          })
          .catch(function(err) {
            this.$message({
              type: "error",
              message: err.message
            });
          });
      }
    }
  },
  watch: {
    show(val) {
      if (!val) {
        this.$emit("closedialog");
      }
    }
  }
};
</script>
<style>
.h-content p {
  line-height: 1.3;
  margin-bottom: 10px;
}
</style>




